package com.wdd.flinkdemo.kafka2mysql;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

public class test {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(5000);
        env.setParallelism(1);
        //模拟kafka数据
        DataStream<String> dataStreamSource = env.addSource(new SourceFunction<String>() {

            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                long c = 0;
                while (true){
                    ctx.collect("test" + c++);
                    Thread.sleep(3000);
                }
            }

            @Override
            public void cancel() {

            }
        }).name("datasource-kafka");
        dataStreamSource.print().setParallelism(1);
//        DataStream<String> stream = dataStreamSource.process(new KafkaDataHandleFunction());

        KeyedProcessFunction keyedProcessFunction = new KafkaDataHandleKeyFunction();
        DataStream stream1 = dataStreamSource.keyBy(new KeySelector<String, Object>() {
            @Override
            public Object getKey(String value) throws Exception {
                return value;
            }
        }).process(keyedProcessFunction);


        //写入数据库
//        stream.addSink(new MySink());
        stream1.addSink(new MySink());
        env.execute("deviceStatusFlinkJob");
    }
}
